<template>
  <el-dialog :model-value="dialogAdd" width="25%" :before-close="handleClose">
    <template #header>
      <h3 style="padding-bottom: 10px">新增标签</h3>
    </template>
    <el-form :model="form" ref="formRef" :rules="rules" label-width="auto">
      <el-form-item label="标签名称" prop="libName">
        <el-input
          v-model="form.libName"
          placeholder="请输入标签名称"
          v-noSpace
        />
      </el-form-item>
      <el-form-item label="标签分类" prop="typeId">
        <el-select
          v-model="form.typeId"
          placeholder="请选择标签分类"
          clearable
          @focus="getClick"
          :loading="loading"
        >
          <el-option
            v-for="item in tagSelectList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分组" prop="tagGroup">
        <el-select v-model="form.tagGroup" placeholder="请选择分组" clearable>
          <el-option label="用户" value="USER" />
          <el-option label="活动" value="ACTIVITY" />
          <el-option label="社群" value="COMMUNITY" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { onMounted, ref } from 'vue';
import { tagAdd, tagSelect } from '@/api/Tag/tag.js';
// 添加用户数据
const form = ref({
  libName: '',
  typeId: '',
  tagGroup: '',
});
const tagSelectList = ref([]);
const loading = ref(false);
// 校验规则
const formRef = ref(null);
const rules = {
  libName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
  typeId: [{ required: true, message: '请选择标签分类', trigger: 'change' }],
  tagGroup: [{ required: true, message: '请选择分组', trigger: 'change' }],
};

// 定义 props，接收父组件传递的数据
defineProps({
  dialogAdd: {
    type: Boolean,
  },
});

// 定义 emits，向父组件发送事件
const emit = defineEmits(['confirm', 'cancel']);

// 处理取消按钮的点击事件
const handleCancel = () => {
  if (formRef.value) {
    formRef.value.resetFields();
  }
  form.value = {};
  emit('cancel');
};

// 处理确认按钮的点击事件
const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      const res = await tagAdd({ ...form.value });
      if (res.data.code === 200) {
        // eslint-disable-next-line no-undef
        ElMessage({
          message: '添加成功',
          type: 'success',
        });
        emit('confirm');
        handleClose();
      } else {
        // eslint-disable-next-line no-undef
        ElMessage({
          message: res.data.msg,
          type: 'error',
        });
      }
    }
  });
};
const getClick = () => {
  getTagSelect();
};
// 获取标签下拉
const getTagSelect = async () => {
  if (tagSelectList.value.length > 0) return; // 如果已有数据则不重复获取
  loading.value = true;

  const res = await tagSelect({});
  if (res.data.code === 200) {
    tagSelectList.value = res.data.data;
    loading.value = false;
  }
};
// 关闭对话框
const handleClose = () => {
  handleCancel();
};
onMounted(() => {});
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
